home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’95 / Menu Controls / UShapes.cp < prev    next >
Encoding:
Text File  |  1995-06-24  |  13.4 KB  |  497 lines  |  [TEXT/MPS ]

  1. // Copyright © 1994-95 by Apple Computer, Inc. All rights reserved.
  2. // UShapes.cp
  3.  
  4. #ifndef __USHAPES__
  5. #include "UShapes.h"
  6. #endif
  7.  
  8. // MacApp
  9.  
  10. #ifndef __UFILE__
  11. #include <UFile.h>
  12. #endif
  13.  
  14. #ifndef __USTREAM__
  15. #include <UStream.h>
  16. #endif
  17.  
  18. #ifndef __UMACAPPUTILITIES__
  19. #include <UMacAppUtilities.h>
  20. #endif
  21.  
  22. #ifndef __UVIEW__
  23. #include <UView.h>
  24. #endif
  25.  
  26. // Toolbox
  27.  
  28. #ifndef __QUICKDRAW__
  29. #include <Quickdraw.h>
  30. #endif
  31.  
  32. #ifndef __FONTS__
  33. #include <Fonts.h>
  34. #endif
  35.  
  36. // DrawShapes
  37.  
  38. #ifndef __PATTERNMENU__
  39. #include "PatternMenu.h"
  40. #endif
  41.  
  42. #ifndef __UDRAWSHAPES__
  43. #include "UDrawShapes.h"
  44. #endif
  45.  
  46. //----------------------------------------------------------------------------------------
  47.  
  48. const short mColor            = 7;            // the Color menu resource id
  49.  
  50. const Boolean kUseOutlineFonts = true;
  51.  
  52. //========================================================================================
  53. // CLASS TShape
  54. //========================================================================================
  55. #undef Inherited
  56. #define Inherited TObject
  57.  
  58. #pragma segment ARes
  59. MA_DEFINE_CLASS_M1(TShape, Inherited);
  60.  
  61. //----------------------------------------------------------------------------------------
  62. // TShape Constructor
  63. //----------------------------------------------------------------------------------------
  64. #pragma segment ARes
  65.  
  66. TShape::TShape()
  67. {
  68.     fIdentifier = 0;
  69.     fLocation = gZeroPt;
  70.     fSize = gZeroPt;
  71.     fPattern = 0;
  72.     fOldPattern = 0;
  73.     fColor = gRGBWhite;
  74.     fIsSelected = false;
  75.     fWasSelected = false;
  76. }
  77.  
  78. //----------------------------------------------------------------------------------------
  79. // TShape::IShape
  80. //----------------------------------------------------------------------------------------
  81. #pragma segment ARes
  82.  
  83. void TShape::IShape(const CRect& itsFrame, short itsID)
  84. {
  85.     this->IObject();
  86.     
  87.     fLocation = itsFrame[topLeft];
  88.     fSize = itsFrame.GetSize();
  89.     
  90.     fIdentifier = itsID;
  91. }
  92.  
  93. //----------------------------------------------------------------------------------------
  94. // TShape::DoInitialState
  95. //----------------------------------------------------------------------------------------
  96. #pragma segment AOpen
  97.  
  98. void TShape::DoInitialState(TShapeView* /*itsView*/)
  99. {
  100.     fPattern = RandomPattern();
  101.     if (qNeedsColorQD || gConfiguration.hasColorQD)
  102.     {
  103.         // Pick a random color from the Color menu
  104.         MCEntryPtr pMCEntry = GetMCEntry(mColor, abs(Random() % CountMItems(GetMenuHandle(mColor))) + 1);
  105.         fColor = pMCEntry->mctRGB2;
  106.     }
  107.     else
  108.         fColor = gRGBBlack;
  109. }
  110.  
  111. //----------------------------------------------------------------------------------------
  112. // TShape::ReadFrom
  113. //----------------------------------------------------------------------------------------
  114. #pragma segment AReadFile
  115.  
  116. void TShape::ReadFrom(TStream* aStream)    // Override
  117. {
  118.     Inherited::ReadFrom(aStream);
  119.  
  120.     fIdentifier = aStream->ReadInteger();
  121.     fLocation = aStream->ReadPoint();
  122.     fSize = aStream->ReadPoint();
  123.     fPattern = aStream->ReadInteger();
  124.     fOldPattern = aStream->ReadInteger();
  125.     aStream->ReadRGBColor(fColor);
  126.     aStream->ReadRGBColor(fOldColor);
  127.     fIsSelected = aStream->ReadBoolean();
  128.     fWasSelected = aStream->ReadBoolean();
  129. }
  130.  
  131. //----------------------------------------------------------------------------------------
  132. // TShape::WriteTo
  133. //----------------------------------------------------------------------------------------
  134. #pragma segment AWriteFile
  135.  
  136. void TShape::WriteTo(TStream* aStream)    // Override
  137. {
  138.     Inherited::WriteTo(aStream);
  139.  
  140.     aStream->WriteInteger(fIdentifier);
  141.     aStream->WritePoint(fLocation);
  142.     aStream->WritePoint(fSize);
  143.     aStream->WriteInteger(fPattern);
  144.     aStream->WriteInteger(fOldPattern);
  145.     aStream->WriteRGBColor(fColor);
  146.     aStream->WriteRGBColor(fOldColor);
  147.     aStream->WriteBoolean(fIsSelected);
  148.     aStream->WriteBoolean(fWasSelected);
  149. }
  150.  
  151. //----------------------------------------------------------------------------------------
  152. // TShape::Draw
  153. //----------------------------------------------------------------------------------------
  154. #pragma segment ANever
  155.  
  156. void TShape::Draw()
  157. {
  158.     this->SubClassResponsibility();
  159. }
  160.  
  161. //----------------------------------------------------------------------------------------
  162. // TShape::DrawOutline
  163. //----------------------------------------------------------------------------------------
  164. #pragma segment ANever
  165.  
  166. void TShape::DrawOutline()
  167. {
  168.     this->SubClassResponsibility();
  169. }
  170.  
  171. //----------------------------------------------------------------------------------------
  172. // TShape::Highlight
  173. //----------------------------------------------------------------------------------------
  174. #pragma segment ARes
  175.  
  176. void TShape::Highlight(HLState fromHL, HLState toHL, TView* itsView)
  177. {
  178.     itsView->SetHLPenState(fromHL, toHL);
  179.  
  180.     // Repaint each shape "handle" in the highlight color
  181.     CRect r(-2, -2, 2, 2);
  182.     CRect extent;
  183.     GetFrame(extent);
  184.  
  185.     // left
  186.     OffsetRect(r, extent.left, (extent.top + extent.bottom) / 2);
  187.     PaintRect(r);
  188.  
  189.     // right
  190.     OffsetRect(r, extent.right - extent.left, 0);
  191.     PaintRect(r);
  192.  
  193.     // top
  194.     OffsetRect(r, -(extent.right - extent.left) / 2, extent.top - ((extent.top + extent.bottom) / 2));
  195.     PaintRect(r);
  196.  
  197.     // bottom
  198.     OffsetRect(r, 0, extent.bottom - extent.top);
  199.     PaintRect(r);
  200. }
  201.  
  202. //----------------------------------------------------------------------------------------
  203. // TShape::SetFields
  204. //----------------------------------------------------------------------------------------
  205. #pragma segment AClipboard
  206.  
  207. void TShape::SetFields(short pattern, CRGBColor color, CRect extentRect)
  208. {
  209.     fPattern = pattern;
  210.     fColor = color;
  211.     SetFrame(extentRect);
  212. }
  213.  
  214. //----------------------------------------------------------------------------------------
  215. // TShape::GetFrame
  216. //----------------------------------------------------------------------------------------
  217. #pragma segment ANever
  218.  
  219. void TShape::GetFrame(CRect& itsFrame)
  220. {
  221.     itsFrame = CRect(fLocation, fLocation + fSize);
  222. }
  223.  
  224. //----------------------------------------------------------------------------------------
  225. // TShape::SetFrame
  226. //----------------------------------------------------------------------------------------
  227. #pragma segment AClipboard
  228.  
  229. void TShape::SetFrame(const CRect& extentRect)
  230. {
  231.     fLocation = extentRect[topLeft];
  232.     fSize = extentRect.GetSize();
  233. }
  234.  
  235. //----------------------------------------------------------------------------------------
  236. // TShape::BeInView
  237. //----------------------------------------------------------------------------------------
  238. #pragma segment ARes
  239.  
  240. void TShape::BeInView(TShapeView* /*itsView*/)
  241. {
  242. }
  243.  
  244. //========================================================================================
  245. // CLASS TArrowTool
  246. //========================================================================================
  247. #undef Inherited
  248. #define Inherited TShape
  249.  
  250. #pragma segment ARes
  251. MA_DEFINE_CLASS_M1(TArrowTool, Inherited);
  252.  
  253. //----------------------------------------------------------------------------------------
  254. // TArrowTool Constructor
  255. //----------------------------------------------------------------------------------------
  256. #pragma segment ARes
  257.  
  258. TArrowTool::TArrowTool()
  259. {
  260. }
  261.  
  262. //----------------------------------------------------------------------------------------
  263. // TArrowTool::IArrowTool
  264. //----------------------------------------------------------------------------------------
  265. #pragma segment ARes
  266.  
  267. void TArrowTool::IArrowTool(CRect itsExtent, short itsID)
  268. {
  269.     this->IShape(itsExtent, itsID);
  270.     
  271.     // Define the arrow bitmap to be drawn in the palette
  272.     fArwBitMap.rowBytes = 2;
  273.     SetRect(&fArwBitMap.bounds, 0, 0, 16, 16);
  274.     fArwBitMap.baseAddr = (Ptr)&qd.arrow.data;
  275. }
  276.  
  277. //----------------------------------------------------------------------------------------
  278. // TArrowTool::Draw
  279. //----------------------------------------------------------------------------------------
  280. #pragma segment ARes
  281.  
  282. void TArrowTool::Draw()
  283. {
  284.     CRect r;
  285.     SetRect(r, 14, 12, 30, 28);
  286.     CopyBits(&fArwBitMap, &(qd.thePort->portBits), &(fArwBitMap.bounds), r, srcOr, NULL);
  287. }
  288.  
  289. //========================================================================================
  290. // CLASS TBox
  291. //========================================================================================
  292. #undef Inherited
  293. #define Inherited TShape
  294.  
  295. #pragma segment ARes
  296. MA_DEFINE_CLASS_M1(TBox, Inherited);
  297.  
  298. //----------------------------------------------------------------------------------------
  299. // TBox Constructor
  300. //----------------------------------------------------------------------------------------
  301. #pragma segment ARes
  302.  
  303. TBox::TBox()
  304. {
  305. }
  306.  
  307. //----------------------------------------------------------------------------------------
  308. // TBox::IBox
  309. //----------------------------------------------------------------------------------------
  310. #pragma segment ARes
  311.  
  312. void TBox::IBox(CRect itsExtent, short itsID)
  313. {
  314.     this->IShape(itsExtent, itsID);
  315. }
  316.  
  317. //----------------------------------------------------------------------------------------
  318. // TBox::Draw
  319. //----------------------------------------------------------------------------------------
  320. #pragma segment ARes
  321.  
  322. void TBox::Draw()
  323. {
  324.     CRect extent;
  325.     GetFrame(extent);
  326.     
  327.     PenNormal();
  328.     if (qNeedsColorQD || gConfiguration.hasColorQD)
  329.     {
  330.         // Get the color of the menu item representing the shape's color
  331.         RGBForeColor(fColor);
  332.         FillRect(extent, &gPat[fPattern]);
  333.         ForeColor(blackColor);
  334.     }
  335.     else
  336.         FillRect(extent, &gPat[fPattern]);
  337.  
  338.     this->DrawOutline();
  339. }
  340.  
  341. //----------------------------------------------------------------------------------------
  342. // TBox::DrawOutline
  343. //----------------------------------------------------------------------------------------
  344. #pragma segment ARes
  345.  
  346. void TBox::DrawOutline()    // Override
  347. {
  348.     PenSize(1, 1);
  349.     CRect extent;
  350.     GetFrame(extent);
  351.     FrameRect(extent);
  352. }
  353.  
  354. //========================================================================================
  355. // CLASS THeavyBox
  356. //========================================================================================
  357. #undef Inherited
  358. #define Inherited TBox
  359.  
  360. #pragma segment ARes
  361. MA_DEFINE_CLASS_M1(THeavyBox, Inherited);
  362.  
  363. //----------------------------------------------------------------------------------------
  364. // THeavyBox Constructor
  365. //----------------------------------------------------------------------------------------
  366. #pragma segment ARes
  367.  
  368. THeavyBox::THeavyBox()
  369. {
  370.     BlockSet((Ptr)&fFiller[0], sizeof(fFiller), 0);
  371. }
  372.  
  373. //----------------------------------------------------------------------------------------
  374. // THeavyBox::IHeavyBox
  375. //----------------------------------------------------------------------------------------
  376. #pragma segment ARes
  377.  
  378. void THeavyBox::IHeavyBox(CRect itsExtent, short itsID)
  379. {
  380.     this->IBox(itsExtent, itsID);
  381. }
  382.  
  383. //----------------------------------------------------------------------------------------
  384. // THeavyBox::ReadFrom
  385. //----------------------------------------------------------------------------------------
  386. #pragma segment AReadFile
  387.  
  388. void THeavyBox::ReadFrom(TStream* aStream)    // Override
  389. {
  390.     Inherited::ReadFrom(aStream);
  391.     aStream->ReadBytes(&fFiller, sizeof(fFiller));
  392. }
  393.  
  394. //----------------------------------------------------------------------------------------
  395. // THeavyBox::WriteToFile
  396. //----------------------------------------------------------------------------------------
  397. #pragma segment AWriteFile
  398.  
  399. void THeavyBox::WriteTo(TStream* aStream)    // Override
  400. {
  401.     Inherited::WriteTo(aStream);
  402.     aStream->WriteBytes(&fFiller, sizeof(fFiller));
  403. }
  404.  
  405. //----------------------------------------------------------------------------------------
  406. // THeavyBox::Draw
  407. //----------------------------------------------------------------------------------------
  408. #pragma segment ARes
  409.  
  410. void THeavyBox::Draw()    // Override
  411. {
  412.     const CStr255 s = "4K";
  413.  
  414.     Inherited::Draw();
  415.  
  416.     PenSize(2, 2);
  417.     CRect extent;
  418.     GetFrame(extent);
  419.     FrameRect(extent);
  420.     PenSize(1, 1);
  421.  
  422.     TextFont(geneva);
  423.     TextFace(normal);
  424.     TextSize(9);
  425.     TextMode(srcOr);
  426.  
  427.     short x = (extent.left + extent.right) / 2;
  428.     CRect r(x - 7, extent.bottom - 14, x + 7, extent.bottom - 4);
  429.     EraseRect(r);
  430.     MADrawString(s, r, teCenter, kUseOutlineFonts);
  431. }
  432.  
  433. //========================================================================================
  434. // CLASS TCircle
  435. //========================================================================================
  436. #undef Inherited
  437. #define Inherited TShape
  438.  
  439. #pragma segment ARes
  440. MA_DEFINE_CLASS_M1(TCircle, Inherited);
  441.  
  442. //----------------------------------------------------------------------------------------
  443. // TCircle Constructor
  444. //----------------------------------------------------------------------------------------
  445. #pragma segment ARes
  446.  
  447. TCircle::TCircle()
  448. {
  449. }
  450.  
  451. //----------------------------------------------------------------------------------------
  452. // TCircle::ICircle
  453. //----------------------------------------------------------------------------------------
  454. #pragma segment ARes
  455.  
  456. void TCircle::ICircle(CRect itsExtent, short itsID)
  457. {
  458.     this->IShape(itsExtent, itsID);
  459. }
  460.  
  461. //----------------------------------------------------------------------------------------
  462. // TCircle::Draw
  463. //----------------------------------------------------------------------------------------
  464. #pragma segment ARes
  465.  
  466. void TCircle::Draw()    // Override
  467. {
  468.     CRect extent;
  469.     GetFrame(extent);
  470.     PenNormal();
  471.     if (qNeedsColorQD || gConfiguration.hasColorQD)
  472.     {
  473.         // Get the color of the menu item representing the shape's color
  474.         RGBForeColor(fColor);
  475.         FillOval(extent, &gPat[fPattern]);
  476.         ForeColor(blackColor);
  477.     }
  478.     else
  479.         FillOval(extent, &gPat[fPattern]);
  480.  
  481.     this->DrawOutline();
  482. }
  483.  
  484. //----------------------------------------------------------------------------------------
  485. // TCircle::DrawOutline
  486. //----------------------------------------------------------------------------------------
  487. #pragma segment ARes
  488.  
  489. void TCircle::DrawOutline()    // Override
  490. {
  491.     PenSize(1, 1);
  492.     CRect extent;
  493.     GetFrame(extent);
  494.     FrameOval(extent);
  495. }
  496.  
  497.